$(document).ready(function() {
	var Main = {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		methods: {
			loginBtn: function() {
				var self = this
				var username = $.trim(self.username);
				var password = $.trim(self.password);
				$.ajax({
					type: "post",
					url: "http://www.ibabypet.com.cn/public/index.php/api/account/login_pwd/",
					async: true,
					data: {
						username: username,
						password: password
					},
					success: function(res) {
						res = JSON.parse(res);
						console.log('res', res)
						if(res.code == 200) {
							self.$message(res.msg)
							self.setCookie('uid', res.data.uid, 1)
							setTimeout(function(){
								window.location.href = 'http://www.ibabypet.com.cn/pcpet/index.html'
							}, 1500)
						} else {
							self.$message(res.msg)
						}
					}
				})
			},
			//设置cookie
			setCookie: function(cname, cvalue, exdays) {
				var d = new Date();
				d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
				var expires = "expires=" + d.toUTCString();
				console.info(cname + "=" + cvalue + "; " + expires);
				document.cookie = cname + "=" + cvalue + "; " + expires;
				console.info(document.cookie);
			}
		}
	}
	var Ctor = Vue.extend(Main)
	new Ctor().$mount('.login_content')
})
var Login = {
	url_api: 'http://www.ibabypet.com.cn/public/index.php/api/account/login_pwd/', //username(登录账号),password(密码)
	login: function() {
		var that = this;
		$('button.login').on('click', function() {
			var username, password;
			username = $.trim($('#user').val());
			password = $.trim($('#uspassworder').val());
			$.ajax({
				type: "post",
				url: that.url_api,
				async: true,
				data: {
					username: username,
					password: password
				},
				success: function(res) {
					res = JSON.parse(res);
					console.log('res', res)
					if(res.code == 200) {

						that.setCookie('uid', res.data.uid, 1)
						window.location.href = 'http://www.ibabypet.com.cn/pcpet/index.html'
					}
				}
			});
		})
	},
	//设置cookie
	setCookie: function(cname, cvalue, exdays) {
		var d = new Date();
		d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
		var expires = "expires=" + d.toUTCString();
		console.info(cname + "=" + cvalue + "; " + expires);
		document.cookie = cname + "=" + cvalue + "; " + expires;
		console.info(document.cookie);
	},
	//获取cookie
	getCookie: function(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while(c.charAt(0) == ' ') c = c.substring(1);
			if(c.indexOf(name) != -1) return c.substring(name.length, c.length);
		}
		return "";
	},
	//清除cookie
	clearCookie: function(username) {
		this.setCookie(username, "", -1);
	},
	checkCookie: function() {
		var user = this.getCookie("username");
		if(user != "") {
			alert("Welcome again " + user);
		} else {
			user = prompt("Please enter your name:", "");
			if(user != "" && user != null) {
				this.setCookie("username", user, 365);
			}
		}
	}

}